﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Manage.Controller;
using GtsServer.Model;
using GtsServer.Common;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// ChanceAjax 的摘要说明
    /// </summary>
    public class ChanceAjax : IHttpHandler
    {
        ChanceController controller = new ChanceController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetList":
                        GetList(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    default:
                        break;
                }
            }
        }

        private void Update(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            int value = Convert.ToInt32(context.Request["chanceValue"].ToString());
            int maxNum = Convert.ToInt32(context.Request["maxNum"].ToString());
            int minNum = Convert.ToInt32(context.Request["minNum"].ToString());
            Chance model = new Chance()
            {
                ID = id,
                ChanceValue = value,
                MaxNum = maxNum,
                MinNum = minNum
            };
            context.Response.Write(controller.UpdateChanceValue(model));
        }

        private void GetList(HttpContext context)
        {
            int typeID = Convert.ToInt32(context.Request["typeID"].ToString());
            List<ChanceShow> list = controller.GetListByTypeID(typeID);
            string json = JsonHelper.ListToJson<ChanceShow>(list);
            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}